using System;
using System.Collections.Generic;
using System.Text;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.DataAccess.Common;
using T.ServiceComponent.Kernel.Service;

namespace T.FAS.Runtime.DataAccess.Engine
{
    /// <summary>
    /// 统一数据访问服务 线程上下文帮助类
    /// </summary>
    internal class DACContextHelper
    {
        #region 时间戳操作
        /// <summary>
        /// 注册时间戳
        /// </summary>
        /// <returns></returns>
        public static string RegistTimestamp()
        {
            string timestampStr = DateTime.Now.ToString(DACConst.FAS_TimestampFormat);
            ContextHelper.Set(DACConst.FAS_Timestamp, timestampStr);
            return timestampStr;
        }

        /// <summary>
        /// 取消注册时间戳
        /// </summary>
        public static void UnRegistTimestamp()
        {
            ContextHelper.GetContext().Context.Remove(DACConst.FAS_Timestamp);
        }

        /// <summary>
        /// 获取线程AsyncLog中的时间戳；
        /// </summary>
        /// <returns></returns>
        public static DateTime GetTimestamp()
        {
            var timestamp = ContextHelper.Get(DACConst.FAS_Timestamp);
            if (string.IsNullOrEmpty(timestamp))
            {
                RegistTimestamp();
                timestamp = ContextHelper.Get(DACConst.FAS_Timestamp);
            }
            if(DateTime.TryParse(timestamp, out DateTime stamp))
            {
                return stamp;
            }
            else
            {
                throw new FasException(DataAccessErrorCode.Error, string.Format(CommonStringClassEngine.DACContextHelper_GetTimestamp,timestamp));
            }
        }

        #endregion

        #region 是否需要校验SDO DO有效性操作
        /// <summary>
        /// 注册是否需要校验SDO DO有效性标志；
        /// </summary>
        public static void RegistIsNeedCheckRefField()
        {
            ContextHelper.Set(DACConst.FAS_IsCheckRefFields, "1");
        }

        /// <summary>
        /// 取消注册是否需要校验SDO DO有效性标志；
        /// </summary>
        public static void UnRegistIsNeedCheckRefField()
        {
            ContextHelper.GetContext().Context.Remove(DACConst.FAS_IsCheckRefFields);
        }

        /// <summary>
        /// 获取是否需要校验SDO DO有效性标志；
        /// </summary>
        /// <returns></returns>
        public static bool GetIsNeedCheckRefField()
        {
            var val = ContextHelper.Get(DACConst.FAS_IsCheckRefFields);
            if (string.IsNullOrEmpty(val))
            {
                return false;
            }
            else
            {
                if (bool.TryParse(val, out bool flag))
                {
                    return flag;
                }
            }
            return false;
        }

        #endregion

        /// <summary>
        /// 批量注册信息到线程上下文中。
        /// 1、包含时间戳，供变更日志以及Update语句使用</summary>
        /// 2、注册是否需要校验SDO DO有效性标志
        /// <param name="exrParams"></param>
        /// <returns>时间戳字符串</returns>
        public static string Regist(Dictionary<string, object> exrParams)
        {
            //注册时间戳，供变更日志以及Update语句使用
            var timestampStr = RegistTimestamp();
            if (DACControlContextHelper.TryGetValue(exrParams, DACConst.ExtParamSwitch.IsCheckRefObject, out bool isCheckRefObject) && isCheckRefObject)
            {
                RegistIsNeedCheckRefField();
            }
            return timestampStr;
        }

        /// <summary>
        /// 批量取消注册
        /// </summary>
        public static void UnRegist()
        {
            //清空时间戳
            UnRegistTimestamp();
            UnRegistIsNeedCheckRefField();
        }
    }
}
